package com.achievo.know.web.util;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.achievo.know.application.SystemConfigApplication;
import com.achievo.know.application.UserApplication;
import com.achievo.know.component.impl.FileManagerConfig;
import com.achievo.know.domain.SystemConfig;
import com.achievo.know.domain.User;
import com.redhat.redwork.coc.RedworkActionSupport;

public class BaseActionSupport extends RedworkActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7049911707060786700L;
	@Autowired
	private SystemConfigApplication systemConfigApplication;
	public void setSystemConfigApplication(
			SystemConfigApplication systemConfigApplication) {
		this.systemConfigApplication = systemConfigApplication;
	}
	@Autowired
	private UserApplication userApplication;
	
	public void setUserApplication(UserApplication userApplication) {
		this.userApplication = userApplication;
	}
	@Autowired
    private FileManagerConfig config;
	
	public void setConfig(FileManagerConfig config) {
		this.config = config;
	}
	private SystemConfig sc;
	private String userId;
	private String userRealName;
	private User user;
	private Map<String,String> auths;
	private List<Long> cateIds;
	private List<Long> adminCateIds;
	
	private String referrer;
	
	public String configValue(String key){
		return config.getConfigHolder().getConfig(key);
	}
	
	public String getUserId() {
		userId=(String) ServletActionContext.getRequest().getSession().getAttribute("loginuser");
		return userId;
	}

	public String getUserRealName() {
		userRealName=(String) ServletActionContext.getRequest().getSession().getAttribute("userRealName");
		return userRealName;
	}
	
	public Map<String, String> getAuths() {
		if(StringUtils.isEmpty(getUserId())){
			return null;
		}
		User loginUser=userApplication.get(getUserId());
		auths=userApplication.findUserAuth(loginUser);//(Map<String,String>)CodeDictCacheHandler.getObject(getUserId()+"_auths");
		return auths;
	}
	
	public boolean isSuperAdmin(){
		if(ServletActionContext.getRequest().getSession().getAttribute("isSuperAdmin")==null)
			return false;
		Boolean isSuperAdmin=(Boolean)ServletActionContext.getRequest().getSession().getAttribute("isSuperAdmin");
		return isSuperAdmin;
	}
	
	public boolean isAdmin(){
		if(ServletActionContext.getRequest().getSession().getAttribute("isAdmin")==null)
			return false;
		Boolean isAdmin=(Boolean)ServletActionContext.getRequest().getSession().getAttribute("isAdmin");
		return isAdmin;
	}
	
	public List<Long> getUserCateIds() {
		if(StringUtils.isEmpty(getUserId())){
			return null;
		}
		User loginUser=userApplication.get(getUserId());
		cateIds=userApplication.findUserCates(loginUser);//(List<Long>) CodeDictCacheHandler.getObject(getUserId()+"_cateIds");
		return cateIds;
	}
	
	public List<Long> getAdminCateIds() {
		if(StringUtils.isEmpty(getUserId())){
			return null;
		}
		User loginUser=userApplication.get(getUserId());
		adminCateIds=userApplication.findAdminCates(loginUser);//(List<Long>) CodeDictCacheHandler.getObject(getUserId()+"_adminCateIds");
		return adminCateIds;
	}

	public boolean hasCate(Long id){
		if(isSuperAdmin()){
			return true;
		}
		cateIds=getUserCateIds();
		if(cateIds==null)
			return false;
		if(cateIds.size()<1)
			return false;
		if(cateIds.contains(id))
			return true;
		return false;
	}
	public boolean hasAuth(String id){
		if(isSuperAdmin()){
			return true;
		}
		auths=getAuths();
		if(auths==null)
			return false;
		if(auths.size()<1)
			return false;
		if(auths.containsKey(id))
			return true;
		return false;
	}
	public User getUser() {
		return user;
	}
	public String getReferrer() {
		HttpServletRequest r=ServletActionContext.getRequest();
		this.referrer=r.getHeader("referer");
		return referrer;
	}

	public void setReferrer(String referrer) {
		this.referrer = referrer;
	}

	
	public SystemConfig getSc() {
		if(sc==null){
			sc=systemConfigApplication.getConfig();
			ServletActionContext.getRequest().setAttribute("sc", sc);
		}else{
			sc=(SystemConfig)ServletActionContext.getRequest().getAttribute("sc");
		}
		return sc;
	}
	public void setAdminCateIds(List<Long> adminCateIds) {
		this.adminCateIds = adminCateIds;
	}
	
	
}
